package com.caifu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: Lyf
 * @Date: 2020/9/6 10:58
 * @Description:
 */
@Mapper
public interface PropertyMapper extends BaseMapper {

    /**
     * 功能描述: 获取物业公司（全部）
     *
     * @auther: Lyf
     * @date: 2020/9/6 10:46
     * @param:
     * @return:
     */
    @Select("select * from wy_company where status = '1'")
    List<Map<String,String>> getCompanyAllList();

    /**
     * 功能描述: 获取物业公司区域
     *
     * @auther: Lyf
     * @date: 2020/9/6 10:46
     * @param:
     * @return:
     */
    @Select("select a.region,b.org_name,case when exists( select 1 from wy_staff_region\tz where a.region = z.region ) then 1 else 0 end as flag from wy_company_region a left join sys_organization b on a.region = b.org_id where a.company_id = #{companyId}")
    List<Map<String,String>> getCompanyRegionList(@Param("companyId") int companyId);

    /**
     * 功能描述: 保存物业负责人
     *
     * @auther: Lyf
     * @date: 2020/9/6 10:48
     * @param:
     * @return:
     */
    @Select("insert into wy_company_staff(CREATE_BY,CREATE_TIME,company_id,duties,name,phone,status,user_no) values(#{createBy},#{createDate},#{companyId},#{post},#{userName},#{phone},'1',#{userNo})")
    void savePropertyUser(@Param("createBy") int createBy, @Param("createDate") Date createDate, @Param("userNo")int userNo, @Param("companyId")int companyId,@Param("post") String post,@Param("userName") String userName,@Param("phone") String phone);

    /**
     * 功能描述: 修改物业负责人
     *
     * @auther: Lyf
     * @date: 2020/9/6 10:50
     * @param:
     * @return:
     */
    @Select("update wy_company_staff set UPDATE_BY = #{updateBy}, CREATE_TIME = #{updateDate},company_id = #{companyId},name = #{userName},phone = #{phone} where user_no = #{userNo}")
    void editPropertyUser(@Param("updateBy") int updateBy, @Param("updateDate") Date updateDate,@Param("userNo")int userNo,@Param("companyId")int companyId,@Param("userName") String userName,@Param("phone") String phone);

    /**
     * 功能描述: 保存物业负责人负责区域
     *
     * @auther: Lyf
     * @date: 2020/9/6 10:50
     * @param:
     * @return:
     */
    @Select("insert into wy_staff_region(staff_id,region) values(#{userNo},#{region})")
    void saveUserRegion(@Param("userNo")int userNo,@Param("region")int region);

    /**
     * 功能描述: 删除物业负责人负责区域
     *
     * @auther: Lyf
     * @date: 2020/9/6 10:51
     * @param:
     * @return:
     */
    @Select("delete from wy_staff_region where staff_id = #{userNo}")
    void deleteUserRegion(@Param("userNo") int userNo);

    /**
     * 功能描述: 删除物业负责人负责区域
     *
     * @auther: Lyf
     * @date: 2020/9/6 10:51
     * @param:
     * @return:
     */
    @Select("update wy_company_staff set status = #{status} where user_no = #{userNo}")
    void updateUserStatus(@Param("userNo") int userNo,@Param("status") String status);

    /**
     * 功能描述: 根据物业公司编号获取物业公司id
     *
     * @auther: Lyf
     * @date: 2020/9/6 14:24
     * @param:
     * @return:
     */
    @Select("select name from wy_company where id = #{companyId}")
    String getCompanyName(@Param("companyId") int companId);

    /**
     * 功能描述: 根据用户编号获取用户区域列表
     *
     * @auther: Lyf
     * @date: 2020/9/6 14:24
     * @param:
     * @return:
     */
    @Select("select * from wy_staff_region where staff_id = #{userNo}")
    List<Map<String,String>> getRegionList(@Param("userNo") int userNo);
}
